//
//  AccountViewController.m
//  project
//
//  Created by 张亚林 on 2017/10/31.
//  Copyright © 2017年 Macmini. All rights reserved.
//

#import "AccountViewController.h"

#import "AccoutTableViewCell.h"
#import "AddAccoutViewController.h"
#import "ZWTools.h"
#import "PaymentModel.h"
#import "NSObject+MJExtension.h"

@interface AccountViewController ()<UITableViewDelegate,UITableViewDataSource>

@property (weak, nonatomic) IBOutlet UITableView *accounttableview;
@property (nonatomic, retain) NSMutableArray *list;

@end

@implementation AccountViewController

- (NSMutableArray *)list {
    
    if (!_list) {
        self.list = @[].mutableCopy;
    }
    
    return _list;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
   
    self.accounttableview.tableFooterView=[UIView new];
    self.accounttableview.delegate = self;
    self.accounttableview.dataSource = self;
}

- (IBAction)clickAddBtn:(id)sender {
    
    AddAccoutViewController *addAccoutVC = [AddAccoutViewController new];
    self.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:addAccoutVC animated:YES];
   
}

- (void)viewWillAppear:(BOOL)animated {
    
    [super viewWillAppear:YES];
    [self getPaymentAddressList];
}

- (void)viewWillDisappear:(BOOL)animated {
    
    [super viewWillDisappear:YES];
    [self.list removeAllObjects];
}

//
- (void)getPaymentAddressList {
    
    ZWTools *tools = [ZWTools sharedManager];
    [tools getPaymentAddressList:nil sucessBlock:^(id jsonResponse) {
        
        NSDictionary *responseDic = (NSDictionary *)jsonResponse;
        NSInteger result = [[responseDic objectForKey:@"code"] integerValue];
        if (result==0) {
            
            NSArray *array = jsonResponse[@"data"]; //判断数组个数是否为空
//            if (array.count==0) {
//                self.list.count=0;
//            } else {
//
//            }
            NSMutableArray *arrayM = [NSMutableArray arrayWithCapacity:array.count];
            for (NSDictionary *dict in array) {
                [self.list addObject:[PaymentModel parse:dict]];
            }
            dispatch_async(dispatch_get_main_queue(), ^{
                [self.accounttableview reloadData];
            });
            
        }else if (result==100) {
            
            
        }
    } Error:^(id jsonResponse) {
        
    }];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    
    return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    return self.list.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    return 162;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    AccoutTableViewCell *cell = [tableView  dequeueReusableCellWithIdentifier:@"accoutTableViewCell"];
    
    if (!cell) {
        cell = [[NSBundle mainBundle] loadNibNamed:@"AccoutTableViewCell" owner:nil options:nil].lastObject;
    }
    
    PaymentModel *model  = self.list[indexPath.row];
    cell.model = model;
    return cell;
}


@end
